package com.sh.business.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.sh.common.idempotent.annotation.RepeatSubmit;
import com.sh.common.log.annotation.Log;
import com.sh.common.web.core.BaseController;
import com.sh.common.mybatis.core.page.PageQuery;
import com.sh.common.core.domain.R;
import com.sh.common.core.validate.AddGroup;
import com.sh.common.core.validate.EditGroup;
import com.sh.common.log.enums.BusinessType;
import com.sh.common.excel.utils.ExcelUtil;
import com.sh.business.domain.vo.PriceTrendsVo;
import com.sh.business.domain.bo.PriceTrendsBo;
import com.sh.business.service.IPriceTrendsService;
import com.sh.common.mybatis.core.page.TableDataInfo;

/**
 * 价格趋势
 *
 * @author ruoyi
 * @date 2025-02-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/priceTrends")
public class PriceTrendsController extends BaseController {

    private final IPriceTrendsService priceTrendsService;

    /**
     * 查询价格趋势列表
     */
    @SaCheckPermission("business:priceTrends:list")
    @GetMapping("/list")
    public TableDataInfo<PriceTrendsVo> list(PriceTrendsBo bo, PageQuery pageQuery) {
        return priceTrendsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出价格趋势列表
     */
    @SaCheckPermission("business:priceTrends:export")
    @Log(title = "价格趋势", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PriceTrendsBo bo, HttpServletResponse response) {
        List<PriceTrendsVo> list = priceTrendsService.queryList(bo);
        ExcelUtil.exportExcel(list, "价格趋势", PriceTrendsVo.class, response);
    }

    /**
     * 获取价格趋势详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:priceTrends:query")
    @GetMapping("/{id}")
    public R<PriceTrendsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(priceTrendsService.queryById(id));
    }

    /**
     * 新增价格趋势
     */
    @SaCheckPermission("business:priceTrends:add")
    @Log(title = "价格趋势", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PriceTrendsBo bo) {
        return toAjax(priceTrendsService.insertByBo(bo));
    }

    /**
     * 修改价格趋势
     */
    @SaCheckPermission("business:priceTrends:edit")
    @Log(title = "价格趋势", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PriceTrendsBo bo) {
        return toAjax(priceTrendsService.updateByBo(bo));
    }

    /**
     * 删除价格趋势
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:priceTrends:remove")
    @Log(title = "价格趋势", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(priceTrendsService.deleteWithValidByIds(List.of(ids), true));
    }
}
